<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\ActivityModel;
use app\admin\model\AreaModel;
use app\admin\model\SuccessModel;

class Activity extends Base
{
    // 文章列表
    public function index()
    {
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            // if (!empty($param['searchText'])) {
            //     $where['title'] = ['like', '%' . $param['searchText'] . '%'];
            // }
            $activity = new ActivityModel();
            $activityList = $activity->getActivityByWhere($where, $offset, $limit);
            foreach($activityList as $key=>&$vo){

                $vo['pic'] = config('product.adminHost').$vo['pic'];
                $vo['showTime'] = date('Y-m-d',$vo['time']);
                $vo['price'] = explode('-', $vo['price']);
                list($start_money, $end_money) = $vo['price'];
                $vo['price'] = $start_money.'元-'.$end_money.'元';

                $vo['showProvinceName'] = getName($vo['province']);
                $vo['showCitylName'] = getName($vo['city']);
                $vo['address'] = $vo['showProvinceName'] .' '.$vo['showCitylName'];
                $activityList[$key]['pic'] = '<img src="' . $vo['pic'] . '" width="40px" height="40px">';
                if($vo['status'] == 1){
                    $activityList[$key]['switch'] = showSwitch($this->makeSwitch($vo['id'],1));
                }else{
                    $activityList[$key]['switch'] = showSwitch($this->makeSwitch($vo['id'],0));
                }
                $activityList[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            unset($vo);

            $return['total'] = $activity->getAllActivity($where);  // 总数据
            $return['rows'] = $activityList;
            return json($return);
        }

        return $this->fetch('activity/index');
    }

        // 文章列表
    public function index1()
    {
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            // if (!empty($param['searchText'])) {
            //     $where['title'] = ['like', '%' . $param['searchText'] . '%'];
            // }
            $activity = new ActivityModel();
            $activityList = $activity->getActivityByWhere($where, $offset, $limit);
            foreach($activityList as $key=>&$vo){
                $vo['pic'] = config('product.adminHost').$vo['pic'];
                $vo['showTime'] = date('Y-m-d',$vo['time']);
                $vo['price'] = explode('-', $vo['price']);
                list($start_money, $end_money) = $vo['price'];
                $vo['price'] = $start_money.'元-'.$end_money.'元';
                $vo['showProvinceName'] = getName($vo['province']);
                $vo['showCitylName'] = getName($vo['city']);
                $vo['address'] = $vo['showProvinceName'] .' '.$vo['showCitylName'];
                $activityList[$key]['pic'] = '<img src="' . $vo['pic'] . '" width="40px" height="40px">';
                if($vo['status'] == 1){
                    $activityList[$key]['switch'] = showSwitch($this->makeSwitch($vo['id'],1));
                }else{
                    $activityList[$key]['switch'] = showSwitch($this->makeSwitch($vo['id'],0));
                }
                $activityList[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            unset($vo);

            $return['total'] = $activity->getAllActivity($where);  // 总数据
            $return['rows'] = $activityList;
            return json($return);
        }

        return $this->fetch('activity/index');
    }
    
    // 添加文章
    public function activityAdd()
    {
        $AreaModel = new AreaModel;
        if(request()->isPost()){
            $param = input('post.');

            unset($param['file']);
            $param['create_time'] = time();
            $param['update_time'] = time();
            $param['starttime'] = strtotime($param['starttime']);
            $param['endtime'] = strtotime($param['endtime']);
            $param['time'] = strtotime($param['time']);
            $Activity = new ActivityModel();
            $flag = $Activity->addActivity($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $this->assign([
            'area' => $AreaModel->getAreaName(),
        ]);
        return $this->fetch();
    }

    public function activityEdit()
    {
        $activity = new ActivityModel();
        $AreaModel = new AreaModel;
        if(request()->isPost()){

            $param = input('post.');
            unset($param['file']);
            $flag = $activity->editActivity($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input('param.id');
        $activityData =  $activity->getOneArticle($id);
        $this->assign('activity',$activityData);
        $this->assign([
            'area' => $AreaModel->getAreaName(),
        ]);
        return $this->fetch();
    }

        public function activityEditStatus()
    {
        $activity = new ActivityModel();
        if(request()->isPost()){
            $param = input('post.');
            if(!$param['id']){
                return json($param);
            }else{
                if($param['status'] == 1){
                    $param['status'] = 0;
                }else{
                    $param['status'] = 1;
                }
                $flag = $activity->editActivity($param);
                return json(msg($flag['code'], $flag['data'], $flag['msg']));
            }
        }
    }

    public function ceshi(){
        $AreaModel = new AreaModel;
        $id = input("get.bigname");
        if(isset($id)){
            $select = $AreaModel->getParentAreaName($id);
            echo json_encode($select); 
        }
    } 

    public function articleDel()
    {
        $id = input('param.id');

        $article = new ArticleModel();
        $flag = $article->delArticle($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    // 上传缩略图
    public function uploadImg()
    {
        if(request()->isAjax()){

            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if($info){
                $src =  '/upload' . '/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }

        // 文章列表
    public function enrollIndex()
    {
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            // if (!empty($param['searchText'])) {
            //     $where['title'] = ['like', '%' . $param['searchText'] . '%'];
            // }
            $activity = new ActivityModel();
            $activityList = $activity->getActivityByWhere($where, $offset, $limit);
            foreach($activityList as $key=>&$vo){

                $vo['pic'] = config('product.adminHost').$vo['pic'];
                $vo['showTime'] = date('Y-m-d',$vo['time']);
                $vo['price'] = explode('-', $vo['price']);
                list($start_money, $end_money) = $vo['price'];
                $vo['price'] = $start_money.'元-'.$end_money.'元';

                $vo['showProvinceName'] = getName($vo['province']);
                $vo['showCitylName'] = getName($vo['city']);
                $vo['address'] = $vo['showProvinceName'] .' '.$vo['showCitylName'];
                $activityList[$key]['pic'] = '<img src="' . $vo['pic'] . '" width="40px" height="40px">';
                $activityList[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            unset($vo);

            $return['total'] = $activity->getAllActivity($where);  // 总数据
            $return['rows'] = $activityList;
            return json($return);
        }

        return $this->fetch();
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'activity/activityedit',
                'href' => url('activity/activityedit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            // '删除' => [
            //     'auth' => 'articles/articledel',
            //     'href' => "javascript:articleDel(" . $id . ")",
            //     'btnStyle' => 'danger',
            //     'icon' => 'fa fa-trash-o'
            // ]
        ];
    }

    private function makeSwitch($id,$status){
        if($status){
            return [
                '是' => [
                    'auth' => 'activity/activityeditstatus',
                    'href' => url('activity/activityeditstatus', ['id' => $id]),
                    'btnStyle' => 'primary',
                    'icon' => 'fa fa-check',
                    'status' => $status,
                    'id' => $id,
                ],
            ];
        }else{
            return [
                '否' => [
                    'auth' => 'activity/activityeditstatus',
                    'href' => url('activity/activityeditstatus', ['id' => $id]),
                    'btnStyle' => 'primary',
                    'icon' => 'fa fa-close',
                    'status' => $status,
                    'id' => $id,
                ],
                // '删除' => [
                //     'auth' => 'articles/articledel',
                //     'href' => "javascript:articleDel(" . $id . ")",
                //     'btnStyle' => 'danger',
                //     'icon' => 'fa fa-trash-o'
                // ]
            ];
        }
    }

        private function makeSwitch1($id,$status){
        if($status){
            return [
                '是' => [
                    'auth' => 'activity/successeditstatus',
                    'href' => "javascript:successeditstatus(" . $id . ")",
                    'btnStyle' => 'primary',
                    'icon' => 'fa fa-check',
                    'status' => $status,
                    'id' => $id,
                ],
            ];
        }else{
            return [
                '否' => [
                    'auth' => 'activity/successeditstatus',
                    'href' => "javascript:successeditstatus(" . $id . ")",
                    'btnStyle' => 'primary',
                    'icon' => 'fa fa-close',
                    'status' => $status,
                    'id' => $id,
                ],
                // '删除' => [
                //     'auth' => 'articles/articledel',
                //     'href' => "javascript:articleDel(" . $id . ")",
                //     'btnStyle' => 'danger',
                //     'icon' => 'fa fa-trash-o'
                // ]
            ];
        }
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton1($id)
    {
        return [
            '编辑' => [
                'auth' => 'activity/successedit',
                'href' => url('activity/successedit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            // '删除' => [
            //     'auth' => 'articles/articledel',
            //     'href' => "javascript:articleDel(" . $id . ")",
            //     'btnStyle' => 'danger',
            //     'icon' => 'fa fa-trash-o'
            // ]
        ];
    }


    // 文章列表
    public function enrollList()
    {
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            $activity = new ActivityModel();
            $activityList = $activity->getEnrollListData($where, $offset, $limit);
            foreach($activityList as $key=>&$vo){
                // $vo['pic'] = config('dev.adminHost').$vo['pic'];
                $vo['showTime'] = date('Y-m-d',$vo['add_time']);
                // $vo['price'] = explode('-', $vo['price']);
                // list($start_money, $end_money) = $vo['price'];
                // $vo['price'] = $start_money.'元-'.$end_money.'元';

                // $vo['showProvinceName'] = getName($vo['province']);
                // $vo['showCitylName'] = getName($vo['city']);
                // $vo['address'] = $vo['showProvinceName'] .' '.$vo['showCitylName'];
                // $activityList[$key]['pic'] = '<img src="' . $vo['pic'] . '" width="40px" height="40px">';
                // $activityList[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            unset($vo);
            $return['total'] = $activity->getAllEnrollList($where);  // 总数据
            $return['rows'] = $activityList;
            return json($return);
        }

        return $this->fetch();
    }

    // 文章列表
    public function successList()
    {
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            // if (!empty($param['searchText'])) {
            //     $where['title'] = ['like', '%' . $param['searchText'] . '%'];
            // }
            $activity = new SuccessModel();
            $activityList = $activity->getScuccessByWhere($where, $offset, $limit);
            // dump($activityList);
            // exit;
            foreach($activityList as $key=>&$vo){
                $vo['pic'] = config('product.adminHost').$vo['pic'];
                $vo['starttime'] = date('Y-m-d',$vo['starttime']);
                $vo['endtime'] = date('Y-m-d',$vo['endtime']);
                $vo['showProvinceName'] = getName($vo['province']);
                $vo['showCitylName'] = getName($vo['city']);
                $vo['address'] = $vo['showProvinceName'] .' '.$vo['showCitylName'];
                $activityList[$key]['pic'] = '<img src="' . $vo['pic'] . '" width="40px" height="40px">';
                if($vo['status'] == 1){
                    $activityList[$key]['switch'] = showSwitch($this->makeSwitch1($vo['id'],$vo['status']));
                }else{
                    $activityList[$key]['switch'] = showSwitch($this->makeSwitch1($vo['id'],$vo['status']));
                }
                $activityList[$key]['operate'] = showOperate($this->makeButton1($vo['id']));
            }
            unset($vo);

            $return['total'] = $activity->getAllActivity($where);  // 总数据
            $return['rows'] = $activityList;
            return json($return);
        }

        return $this->fetch();
    }

    public function successEditStatus()
    {
        $activity = new successModel();
        if(request()->isPost()){
            $param = input('post.');
            if(!$param['id']){
                return json($param);
            }else{
                if($param['status'] == 1){
                    $param['status'] = 0;
                }else{
                    $param['status'] = 1;
                }
                $flag = $activity->editStory($param);
                return json(msg($flag['code'], $flag['data'], $flag['msg']));
            }
        }
    }

    public function successEdit()
    {
        $activity = new successModel();
        $AreaModel = new AreaModel;
        if(request()->isPost()){

            $param = input('post.');
            $param['starttime'] = strtotime($param['starttime']);
            $param['endtime'] = strtotime($param['endtime']);
            // dump($param);
            // exit;
            unset($param['file']);
            $flag = $activity->editSuccess($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input('param.id');
        $activityData =  $activity->getOneArticle($id);
        $this->assign('activity',$activityData);
        $this->assign([
            'area' => $AreaModel->getAreaName(),
        ]);
        return $this->fetch();
    }
}
